class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();
        vector<int> arr1(n, 1), arr2(n, 1), ret(n);
        for(int left = 1, right = n-2; left < n; ++left, --right)
        {
            arr1[left] = arr1[left-1] * nums[left-1]; // 前缀积
            arr2[right] = arr2[right+1] * nums[right+1]; // 后缀积
        }
        for(int i = 0; i < n; ++i)
        {
            ret[i] = arr1[i] * arr2[i];
        }
        return ret;
    }
};